<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Bon_de_commande_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /*     * *************************************************************************
     * Accesseurs
     * ************************************************************************ */

    public function getList($SOCIETE_ID, $COMMANDE_CLIENT) {
        return $this->db
                        ->select('COMMANDE_ID, SOCIETE_ID, COMMANDE_DATE, COMMANDE_CLIENT')
                        ->from('BON_COMMANDE')
                        ->where('SOCIETE_ID', $SOCIETE_ID)
                        ->where('COMMANDE_CLIENT', $COMMANDE_CLIENT)
                        ->get()
                        ->result();
    }

    public function get($COMMANDE_ID) {
        $result = $this->db
                ->select('COMMANDE_ID, SOCIETE_ID, COMMANDE_DATE, COMMANDE_CLIENT')
                ->from('BON_COMMANDE')
                ->where('COMMANDE_ID', $COMMANDE_ID)
                ->get()
                ->result();

        if (count($result) == 1) {
            $result = $result[0];
        }

        return $result;
    }

    /*     * *************************************************************************
     * Méthodes d'enregistrement
     * ************************************************************************ */

    public function create($SOCIETE_ID, $COMMANDE_DATE, $COMMANDE_CLIENT) {
        $date = new DateTime('@' . $COMMANDE_DATE, new DateTimeZone('Europe/Paris'));
        $COMMANDE_DATE = $date->format('Y-m-d H:i:s');

        $this->db->set('SOCIETE_ID', $SOCIETE_ID)
                ->set('COMMANDE_DATE', $COMMANDE_DATE)
                ->set('COMMANDE_CLIENT', $COMMANDE_CLIENT)
                ->insert('BON_COMMANDE');

        return $this->db->insert_id();
    }

    public function update($COMMANDE_ID, $SOCIETE_ID, $COMMANDE_DATE, $COMMANDE_CLIENT) {
        $data = array(
            'SOCIETE_ID' => $SOCIETE_ID,
            'COMMANDE_DATE' => $COMMANDE_DATE,
            'COMMANDE_CLIENT' => $COMMANDE_CLIENT
        );
        $this->db->where('COMMANDE_ID', $COMMANDE_ID)
                ->update('BON_COMMANDE', $data);
    }

    public function Delete($COMMANDE_ID) {
        $this->db->where('COMMANDE_ID', $COMMANDE_ID)
                ->delete('BON_COMMANDE');
    }

    public function pdf($COMMANDE_ID, $SOCIETE_ID) {
        $ent_com = $this->db->select(array('SOCIETE.SOCIETE_NOM', 'BON_COMMANDE.COMMANDE_DATE'))
                ->from('SOCIETE')
                ->join('BON_COMMANDE', 'BON_COMMANDE.SOCIETE_ID = SOCIETE.SOCIETE_ID')
                ->where('BON_COMMANDE.COMMANDE_ID = ' . $COMMANDE_ID . '')
                ->get()
                ->result();

        $comm = $this->db->select(array('ARTICLE.*', 'DETAIL_COMMANDE.DETAIL_QUANTITE', 'SOCIETE_ARTICLE.PRIX_UNITAIRE'))
                ->from('ARTICLE')
                ->join('DETAIL_COMMANDE', 'DETAIL_COMMANDE.ARTICLE_ID = ARTICLE.ARTICLE_ID')
                ->join('SOCIETE_ARTICLE', 'ARTICLE.ARTICLE_ID = SOCIETE_ARTICLE.ARTICLE_ID')
                ->where('DETAIL_COMMANDE.COMMANDE_ID = ' . $COMMANDE_ID . '')
                ->get()
                ->result();
        
        $soc = $this->db->select('*')
                        ->from('ADRESSE')
                        ->where('SOCIETE_ID', $SOCIETE_ID)
                        ->get()
                        ->result();
        
        ob_start();
        $page = '<page  format="A5" pageset="new" footer="date" backimg="assets/images/bon_de_commande.jpg" backimgx="left" backimgy="top" backimgw="100%" backimgh="100%">';
        
        $page .= '<div style="width: 100%;">';
        $page .= '<h1 style="text-align: center;">BON DE COMMANDE</h1>';
        $page .= '</div>';
        
        $page .= '<table style="width:100%;">';
        $page .= '<thead>';
        $page .= '<tr>';
        $page .= '<th></th>';
        $page .= '<th></th>';
        $page .= '</tr>';
        $page .= '</thead>';
        $page .= '<tbody>';
        $page .= '<tr>';
        
        $date = explode('-', $ent_com[0]->COMMANDE_DATE);
        $day = substr($date[2], 0, 2);
        $page .= '<td style="width:65%;">' . $day.'/'.$date[1].'/'.$date[0] . '</td>';   
        $page .= '<td style="width:35%;">' . $soc[0]->ADRESSE_NUMVOIE . ' ' . $soc[0]->ADRESSE_NOMVOIE . '</td>';
        $page .= '</tr>';
        $page .= '<tr>';
        $page .= '<td style="width:65%;">' . $ent_com[0]->SOCIETE_NOM . '</td>';   
        $page .= '<td style="width:35%;">' . $soc[0]->ADRESSE_CODEPOSTAL . ' ' . $soc[0]->ADRESSE_VILLE . '</td>';
        $page .= '</tr>';
        $page .= '<tr>';
        $page .= '<td style="width:65%;"></td>';   
        $page .= '<td style="width:35%;">' . $soc[0]->ADRESSE_PAYS . '</td>';
        $page .= '</tr>';
        $page .= '</tbody>';
        $page .= '</table>';
        
        $page .= '<br />';
        $page .= '<table border="1" style="width:100%;">';
        $page .= '<thead>';
        $page .= '<tr>';
        $page .= '<th>Référence</th>';
        $page .= '<th>Libellé</th>';
        $page .= '<th>Quantité</th>';
        $page .= '<th>Prix</th>';
        $page .= '</tr>';
        $page .= '</thead>';
        $page .= '<tbody>';
        
        $quantite_total = null;
        foreach ($comm as $values) {
            $page .= '<tr>';
            $page .= '<td style="width:20%;">' . $values->ARTICLE_REF . '</td>';
            $page .= '<td style="width:60%;">' . $values->ARTICLE_LIBELLE . '</td>';
            $page .= '<td style="width:10%;">' . $values->DETAIL_QUANTITE . '</td>';
            $page .= '<td style="width:10%;">' . $values->PRIX_UNITAIRE . '</td>';
            $page .= '</tr>';
            
            $quantite_total += $values->DETAIL_QUANTITE * $values->PRIX_UNITAIRE;
        }
          
        $page .= '</tbody>';
        $page .= '</table>';
        
        $page .= '<br />';
        
        $page .= '<table style="width:100%;">';
        $page .= '<thead>';
        $page .= '<tr>';
        $page .= '<th></th>';
        $page .= '<th></th>';
        $page .= '</tr>';
        $page .= '</thead>';
        $page .= '<tbody>';
        $page .= '<tr>';
        $page .= '<td style="width:70%;"></td>';  
        $page .= '<td style="width:30%;">Quantité total: ' . $quantite_total . '</td>';
        $page .= '</tr>';
        $page .= '</tbody>';
        $page .= '</table>';
        $page .= '</page>';

        $content = '';
        $content .= $page;

        $this->load->library('htmlToPdf');
        $html2pdf = new HTML2PDF('P', 'A5', 'fr');
        $html2pdf->WriteHTML($content);
        $html2pdf->Output('assets/BC_.pdf');
    }

}

?>